/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.butnet.myframe.servlet;

import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Administrator
 */
public class WordServlet extends HttpServlet{
	private static final long serialVersionUID = 1L;

	@Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        process(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        process(req,resp);
    }

    private void process(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String url = req.getParameter("url");
        if(url==null){
            resp.setStatus(HttpServletResponse.SC_NOT_FOUND);
            resp.getWriter().println("parameter 'url' is not found !");
            return;
        }
        String fileName = req.getParameter("name");
        if(fileName==null)fileName="word.doc";
        RequestDispatcher rd = req.getRequestDispatcher(url);
        if(rd==null){
            resp.setStatus(HttpServletResponse.SC_NOT_FOUND);
            resp.getWriter().println("file: "+url+" is not found !");
            return;
        }
        resp.setContentType("application/word");
        resp.setHeader("Content-Disposition", "attachment;filename=\"" + new String(fileName.getBytes(), "iso8859-1").replaceAll(" ", "_") + "\"");
        rd.forward(req, resp);
    }
}
